package com.deere.workprocedure.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.deere.dao.GenericDao;
import com.deere.model.ItemWorkflow;
import com.deere.model.WorkOrder;
import com.deere.model.WorkingProcedure;
import com.deere.model.enums.WorkOrderStatus;
/**
 * 
 * @author zhaohongxing
 *
 */
@Service
public class ItemWorkflowService {
	
	@Autowired
	private GenericDao<ItemWorkflow> workflowDao;
	
	@Autowired
	private GenericDao<WorkingProcedure> workingProcedureDao;
	
	@Autowired
	private GenericDao<WorkOrder> workOrderDao;
	/**
	 * 计算工序所需的费用
	 * @return
	 */
	public double calcWorkingProcedurePrice(){
		return 0;
	}
	/**
	 * 获取零件的加工流程
	 * @param partCode
	 * @return
	 */
	public List<ItemWorkflow> getItemWorkflows(String partCode){
		String query = "from ItemWorkflow where partCode = '"+partCode+"' order by sequence asc";
		
		List<ItemWorkflow> workflows = null;
		try {
			workflows = workflowDao.query(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workflows;
	}
	/**
	 * 获取零件的加工流程
	 * @param partCode
	 * @return
	 */
	public ItemWorkflow getItemWorkflow(String partCode){
		String query = "from ItemWorkflow where partCode = '"+partCode+"' order by sequence";

		List<ItemWorkflow> workflows = null;
		workflows = workflowDao.query(query);
		return (null == workflows ? null : workflows.get(0));
	}
	/**
	 * 获取<code>partCode</code>对应零件加工流程的最后一个工序
	 * @param partCode
	 * @return
	 */
	public ItemWorkflow getLastItemWorkflow(String partCode){
		String query = "from ItemWorkflow where sequence = ( select max(sequence) from ItemWorkflow where partCode = '"+partCode+"' )";
		
		List<ItemWorkflow> workflows = null;
		workflows = workflowDao.query(query);
		return (null == workflows ? null : workflows.get(0));
	}
	/**
	 * 获取<code>partCode</code>对应零件加工流程中的首个工序
	 * @param partCode
	 * @return
	 */
	public ItemWorkflow getFirstItemWorkflow(String partCode){
		String query = "from ItemWorkflow where sequence = ( select min(sequence) from ItemWorkflow where partCode = '"+partCode+"' )";
		
		List<ItemWorkflow> workflows = null;
		workflows = workflowDao.query(query);
		return (null == workflows ? null : workflows.get(0));
	}
	/**
	 * 
	 * @param workflow
	 * @return
	 */
	public int getMaxWPCodeSequence(ItemWorkflow workflow){
		String query = "from ItemWorkflow iw where wpCode = '"+workflow.getWpCode()+"' and partCode = '"+workflow.getPartCode()+"' order by sequence desc";

		List<ItemWorkflow> workflows = null;
		try {
			workflows = workflowDao.query(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int max = 1;
		if(workflows != null && workflows.size() > 0){
			workflow = workflows.get(0);
			max = workflow.getSequence()+10;
		}
		return max;
	}
	/**
	 * 保存一个零件的加工工序
	 * @param itemWorkflow
	 */
	public void insertItemWorkflow(ItemWorkflow itemWorkflow){
		workflowDao.save(itemWorkflow);
	}
	/**
	 * 保存零件的整个加工流程
	 * @param itemWorkflow
	 */
	public void insertItemWorkflows(List<ItemWorkflow> itemWorkflows){
		for(int i = 0; i < itemWorkflows.size(); i++){
			ItemWorkflow iw = itemWorkflows.get(i);
			iw.setSequence(i*10);
			insertItemWorkflow(iw);
		}
	}
	
	public void removeItemWorkflow(ItemWorkflow itemWorkflow){
		workflowDao.delete(itemWorkflow);
	}
	/**
	 * 根据workflow来生成其对应的工序对象
	 * @param workflow
	 */
	public void generateWorkingProcedure(ItemWorkflow workflow ){
		List<ItemWorkflow> workflows = getItemWorkflows(workflow.getPartCode());
		for(ItemWorkflow wf : workflows){
			WorkOrder order = new WorkOrder();
			order.setPartCode(wf.getPartCode());
			order.setWpCode(wf.getWpCode());
			order.setStatus(WorkOrderStatus.NEW.toString());
			workOrderDao.save(order);
		}
	}
	/**
	 * 导入零件的整个加工流程
	 */
	public void importItemWorkflow(){
		
	}
}
